package com.cdm.beans;

import java.util.List;

import net.sf.frails.actions.Scrollable;
import net.sf.frails.actions.ScrollableAction;
import net.sf.frails.aop.annotations.InjectBean;
import net.sf.frails.bean.annotations.DefMbean;
import net.sf.frails.bean.annotations.InVar;
import net.sf.frails.bean.annotations.Prop;

import com.cdm.persistence.EntityManagerHelper;
import com.cdm.persistence.User;

@Scrollable(span = 3)
@DefMbean
public class UserListBean extends ScrollableAction {

	@InVar
	@Prop
	private User user;

	@SuppressWarnings("unchecked")
	public List<User> getDataList() {
		List<User> list = EntityManagerHelper.createQuery("Select user from User user").getResultList();
		System.out.println("\nlist: " + list + "\n");
		return list;
	}

	@Override
	public int getDataSize() {
		int size = ((Long) EntityManagerHelper.createQuery("select count(*) from User").getSingleResult()).intValue();
		System.out.println("\nsize: " + size + "\n");
		return size;
	}

	@InjectBean
	public String update() {
		setExpressionValue("newUser.user", user);
		setExpressionValue("newUser.create", false);
		return "newUser";
	}

	public String create() {
		setExpressionValue("newUser.create", true);
		return "newUser";
	}

	public String getName() {
		System.out.println("get name!!!!!!!!!!!!!!!!!!!!!");
		return "Cain";
	}

	public static void main(String[] args) {
		UserListBean action = new UserListBean();
		List<User> list = action.getDataList();
		for (User user : list) {
			System.out.println(user);
		}
	}
}
